//
//  ShareController.m
//  032519-友盟统计SDK
//
//  Created by 强进冬 on 2019/3/27.
//  Copyright © 2019 强进冬. All rights reserved.
//

#import "ShareController.h"
#import "UMTool.h"

typedef NS_ENUM(NSInteger, ShareMessageType) {
    ShareMessageTypeNone = -1,
    ShareMessageTypeText,
    ShareMessageTypePicture,
    ShareMessageTypeImageText,
    ShareMessageTypeWebpage,
    ShareMessageTypeMusic,
    ShareMessageTypeVideo,
    ShareMessageTypeWechatEmotion,
    ShareMessageTypeWXMiniProgram,
};

@interface ShareController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 数据源 */
@property (nonatomic, strong) NSArray<NSString *> *dataSource;
/** 分享消息类型 */
@property (nonatomic, assign) NSInteger selectedShareMessageIndex;
/** 分享消息类型数组 */
@property (nonatomic, strong) NSArray<NSString *> *shareMessageTypeArray;
@end

static NSString * const ShareCellID = @"ShareCellID";

@implementation ShareController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"分享";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:UITableViewCell.self forCellReuseIdentifier:ShareCellID];
    self.tableView.tableFooterView = [UIView new];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ShareCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@%@", self.dataSource[indexPath.section], self.shareMessageTypeArray[self.selectedShareMessageIndex]];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataSource[indexPath.section]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [self selectShareMessageType];
    } else if (indexPath.section == 1) {
        // 选择分享菜单面板
        ShareMessageType shareMessageType = self.selectedShareMessageIndex;
        UMSocialMessageObject *msgObject;
        switch (shareMessageType) {
            case ShareMessageTypeNone:
                break;
            case ShareMessageTypeText:
                msgObject = [UMTool messageWithText:@"这是一条文本分享"];
                break;
            case ShareMessageTypePicture:
                msgObject = [UMTool messageWithPicture:[UIImage imageNamed:@"music"]
                                          pictureArray: @[
                                                          [UIImage imageNamed:@"music"],
                                                          [UIImage imageNamed:@"music"],
                                                          ]
                                                  text:nil
                                            thumbImage:@"https://qjd-images.oss-cn-hangzhou.aliyuncs.com/18-11-20/25850778.jpg"];
                break;
            case ShareMessageTypeImageText:
                msgObject = [UMTool messageWithPicture:[UIImage imageNamed:@"music"] pictureArray:nil text:@"图文分享" thumbImage:nil];
                break;
            case ShareMessageTypeWebpage:
                msgObject = [UMTool messageWithWebpageUrlString:@"http://music.163.com/" title:@"title" description:@"describe" thumImage:nil];
                break;
            case ShareMessageTypeMusic:
                msgObject = [UMTool messageWithMusic:@"http://c.y.qq.com/v8/playsong.html?songid=108782194&source=yqq#wechat_redirect" title:@"title" description:@"description" thumImage:[UIImage imageNamed:@"music"]];
                break;
            case ShareMessageTypeVideo:
                msgObject = [UMTool messageWithVideo:@"http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html" title:@"title" description:@"description" thumImage:[UIImage imageNamed:@"music"]];
                break;
            case ShareMessageTypeWechatEmotion:
                msgObject = [UMTool messageWithText:@"这是一条文本分享"];
                break;
            case ShareMessageTypeWXMiniProgram:
                msgObject = [UMTool messageWithText:@"这是一条文本分享"];
                break;
        }
        
        // 配置分享面板
//        [UMTool shareMenuBarConfigure];
        
        // 添加自定义分享平台
        [UMTool shareMenuBarAddCustomPlatform];
        
        // 显示分享面板
        [UMTool showShareMenuWithDefinePlatforms:nil completion:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            if (platformType == UMSocialPlatformType_UserDefine_Begin + 1) {
                NSLog(@"点击了自定义平台");
                return ;
            }
            [self shareToPlatform:platformType messageObject:msgObject];
        }];
    }
}

#pragma mark - private method
/**
 选择分享类型
 */
- (void)selectShareMessageType {
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"选择要分享消息体的类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消分享");
    }];
    [sheet addAction:cancel];
    
    for (int i = 0; i < self.shareMessageTypeArray.count; i ++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:self.shareMessageTypeArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.selectedShareMessageIndex = i;
            [self.tableView reloadData];
        }];
        [sheet addAction:action];
    }
    
    [self presentViewController:sheet animated:YES completion:nil];
}


/**
 分享
 @param platformType <#platformType description#>
 @param messageObject <#messageObject description#>
 */
- (void)shareToPlatform:(UMSocialPlatformType)platformType messageObject:(UMSocialMessageObject *)messageObject {
    // 调用分享接口
    [UMTool shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

#pragma mark - lazy
- (NSArray<NSString *> *)dataSource {
    if (!_dataSource) {
        _dataSource = @[@"分享消息类型: ", @"调用分享面板", ];
    }
    return _dataSource;
}

- (NSArray<NSString *> *)shareMessageTypeArray {
    if (!_shareMessageTypeArray) {
        _shareMessageTypeArray = @[
                                   @"文本",
                                   @"图片",
                                   @"图文",
                                   @"网页",
                                   @"音乐",
                                   @"视频",
                                   @"微信表情",
                                   @"微信小程序",
                                   ];
    }
    return _shareMessageTypeArray;
}

@end
